libxl: Crash (more sensibly) on malloc failure
authorIan Jackson <ian.jackson@eu.citrix.com>
Wed, 11 Apr 2012 13:14:13 +0000 (14:14 +0100)
committerIan Jackson <ian.jackson@eu.citrix.com>
Wed, 11 Apr 2012 13:14:13 +0000 (14:14 +0100)
commitb77efdf32a0f4b68a5229757b0ee67fbcd81b703
treed7011caa4d0af898301d1087a32c794f1b3a2e23
parent9c09f9dec695c7035a4ab6a338e767d0d8a54b0c
libxl: Crash (more sensibly) on malloc failure

Formally change the libxl memory allocation failure policy to "crash".

Previously we had a very uneven approach; much code assumed that
libxl__sprintf (for example) would never return NULL, but some code
was written more carefully.

We think it is unlikely that we will be able to make the library
actually robust against allocation failure (since that would be an
awful lot of never-tested error paths) and few calling environments
will be able to cope anyway.  So, instead, adopt the alternative
approach: provide allocation functions which never return null, but
will crash the whole process instead.

Consequently,
 - New noreturn function libxl__alloc_failed which may be used for
   printing a vaguely-useful error message, rather than simply
   dereferencing a null pointer.
 - libxl__ptr_add now returns void as it crashes on failure.
 - libxl__zalloc, _calloc, _strdup, _strndup, crash on failure using
   libxl__alloc_failed.  So all the code that uses these can no longer
   dereference null on malloc failure.

While we're at it, make libxl__ptr_add use realloc rather than
emulating it with calloc and free, and make it grow the array
exponentially rather than linearly.

Things left to do:
 - Remove a lot of now-spurious error handling.
 - Remove the ERROR_NOMEM error code.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
Committed-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
tools/libxl/libxl.h
tools/libxl/libxl_internal.c
tools/libxl/libxl_internal.h